﻿using System.Collections.Generic;
using System.Linq;
using Game.Map;
using Game.Objects;
using Game.Plugin;
using Game.Plugin.Parse;
using Game.Map.Extensions;

namespace DebugPlugin
{
    public class VehiclesPlugin : AbstractPlugin
    {
        readonly IGameMapView _GameMapView;

        public VehiclesPlugin(
            IGamePlugin parent,
            IGameMapView view)
            : base(parent, "vehicles")
        {
            _GameMapView = view;
        }

        public override ICodeObject Parse(
            string token, 
            ParserContext context)
        {
            IExpression e = new VehiclesExpression(_GameMapView);
            context.Stack.Push(e);
            return e;
        }
    }

    class VehiclesExpression : IExpression
    {
        readonly IGameMapView _GameMapView;

        public VehiclesExpression(IGameMapView view)
        {
            _GameMapView = view;
        }

        public object Interpret(Dictionary<string, ICodeObject> variables)
        {
            var list = new List<Vehicle>();
            foreach (var f in _GameMapView.ViewFields)
            {
                if (f.VehicleX().Vehicle != null)
                {
                    list.Add(f.VehicleX().Vehicle);
                }
            }

            return list.Aggregate("", (current, e) => current + ("(" + e + ")"));
        }
    }
}
